package com.ld.explanation;

import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.impl.EditorImpl;

public class ZHExplanation extends TextExplanation{

    public ZHExplanation(Editor editor) {
        this.editor = editor;
    }

    @Override
    public String translation() {
        String selectedText = getSelectedText();
        String cache = translationCache.get(selectedText);
        if (cache != null){
            return cache;
        }
        String[] strings = selectedText.split("[a-zA-Z]");
        for (String str:strings){
            int length = str.trim().length();
            if (length == 0){
                continue;
            }
            try {
                String zhToEn = zhToEn(str);
                selectedText = selectedText.replace(str," "+zhToEn+" ");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (!isAComment()){
            Interval interval = autoInterval();
            switch (interval){
                case HUMP:
                    selectedText = hump(selectedText);
                    break;
                case UNDERLINE:
                    selectedText = underline(selectedText);
                    break;
                case SPACE:
                    break;
                default:
                    break;
            }
            selectedText = selectedText.trim();
            switch (interval){
                case HUMP:case UNDERLINE:
                    if (selectedText.lastIndexOf(".") != -1){
                        selectedText = selectedText.substring(0,selectedText.length()-1);
                    }
                    break;
                case SPACE:
                    break;
                default:
                    break;
            }
        }
        translationCache.put(getSelectedText(),selectedText);
        translationCache.put(selectedText,getSelectedText());
        return selectedText;
    }



    public Interval autoInterval(){
        EditorImpl editorImpl = (EditorImpl)editor;
        String name = editorImpl.getVirtualFile().getName();
        String suffixName = name.substring(name.lastIndexOf(".")+1);
        String textAll = editorImpl.getDocument().getText();
        String prefixText = textAll.substring(0,editorImpl.getCaretModel().getOffset());
        prefixText = prefixText.substring(prefixText.lastIndexOf("\n"));
        String suffixText = textAll.substring(editorImpl.getCaretModel().getOffset());
        suffixText = suffixText.substring(0,suffixText.indexOf("\n"));
        String lineText = prefixText + suffixText;
        lineText = lineText.trim();
        if (lineText.startsWith("//")
                || lineText.startsWith("/*")
                || lineText.startsWith("*")
                || lineText.startsWith("`")
                || lineText.startsWith("'")
                || lineText.startsWith("\"")){//常见注释
            return Interval.SPACE;
        }else{
            lineText = lineText.replace(getSelectedText(),"CUT");
            String[] split = lineText.split("CUT");
            if (split.length == 2){
                char c_1 = split[0].charAt(split[0].length() - 1);
                char c_2 = split[1].charAt(0);
                if (c_1 == '"' && c_2 == '"'){
                    return Interval.SPACE;
                }else if (c_1 == '\'' && c_2 == '\''){
                    return Interval.SPACE;
                }
            }
            switch (suffixName){
                case "java":
                   return Interval.HUMP;
                case "py":case "go":case "html":case "js":case "css":
                    return Interval.UNDERLINE;
                default:
                    return Interval.SPACE;
            }
        }
    }



    private boolean isAComment(){
        String text = editor.getDocument().getText();
        text = text.substring(0,editor.getCaretModel().getOffset());
        String[] split = text.split("\n");
        String s1 = split[split.length - 1];
        String trim = s1.trim();
        boolean b = trim.startsWith("//");
        if (!b){
            b = trim.startsWith("*");
            if (!b){
                b = trim.startsWith("/*");
            }
        }
        return b;
    }

    private String hump(String v){
        StringBuilder sb = new StringBuilder();
        for (String s : v.split(" ")) {
            String trim = s.trim();
                trim = trim.toLowerCase();
            if (trim.length() == 0){
                continue;
            }
            if (sb.length() > 0){
                trim = upperCase(trim);
            }
            sb.append(trim);
        }
        return sb.toString();
    }

    private String underline(String v){
        StringBuilder sb = new StringBuilder();
        for (String s : v.split(" ")) {
            String trim = s.trim();
            trim = trim.toLowerCase();
            if (trim.length() == 0){
                continue;
            }
            sb.append(trim).append("_");
        }
        String s = sb.toString();
        return s.substring(0,s.length()-1);
    }

    private String upperCase(String str) {
        char[] ch = str.toCharArray();
        if (ch[0] >= 'a' && ch[0] <= 'z') {
            ch[0] = (char) (ch[0] - 32);
        }
        return new String(ch);
    }
}

enum Interval{
    HUMP,
    SPACE,
    UNDERLINE,
}
